<template>
  <ContentWrap>
    <fromTitle title="检验信息审批" :queryParams="queryParams" @search="getList" @reset="resetQuery" /> 

    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
    >


      <el-form-item label="样品名称" prop="sampleName">
        <el-input
          v-model="queryParams.sampleName"
          placeholder="请输入样品名称"
          clearable
          @keyup.enter="handleQuery"
         class="!w-240px"
        />
      </el-form-item>

      <el-form-item  label="样品编号" prop="sampleNum">
        <el-input
          v-model="queryParams.sampleNum"
          placeholder="请输入样品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>


      <el-form-item  label="检验状态" prop="approveState">
                       <!-- <el-input v-model="formData.projectManager" placeholder="请输入未检查/开始检查/检验完成/重新检验" /> -->
                       <el-select
          v-model="queryParams.approveState"
          placeholder="请选择检验状态"
          clearable
          class="!w-240px"
        >
          <el-option   v-for="(item) of statusList" :key="item.value " :label="item.label" :value="item.value" />
          </el-select>
      </el-form-item>
<!-- 
      <el-form-item  label="样品编号" prop="sampleNum">
        <el-input
          v-model="queryParams.sampleNum"
          placeholder="请输入样品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->

      
      <el-form-item label="抽样时间" prop="samplingStartDate1">
        <el-date-picker
          v-model="queryParams.samplingStartDate1"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          clearable
        />
      </el-form-item>


      <el-form-item label="检验时间"  prop="checkFilterEndTime1">
        <el-date-picker
          v-model="queryParams.checkFilterEndTime1"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>

      </el-form-item> -->
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
    <el-button type="info" @click="handleExport">导出</el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox v-model="scope.row.selected" @change="handleSelect(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="样品名称" align="center" prop="sampleName">
        <template #default="scope">
          <el-link type="primary" @click="handleViewDetails(scope.row)">
            {{ scope.row.sampleName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="样品编号" align="center" prop="sampleNum" />
      <el-table-column label="规格型号" align="center" prop="model" />
      <el-table-column label="执行标准" align="center" prop="executiveStandard" />
      <el-table-column label="抽样地点" align="center" prop="samplingSite" />
      <el-table-column label="抽样时间" align="center" prop="samplingDate" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="承检机构" align="center" prop="orgName" />
      <el-table-column label="检验状态" align="center" prop="approvalType" />

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrganizationQualificationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrganizationQualificationApi, OrganizationQualificationVO } from '@/api/qis/organizationqualification'
import OrganizationQualificationForm from './OrganizationQualificationForm.vue'
import { useRouter } from 'vue-router'
import { SampleInformationApi } from '@/api/qis/sampleinformation'
import { getDictDataPage } from '@/api/system/dict/dict.data'

/** 质检平台-承检资质 列表 */
defineOptions({ name: 'OrganizationQualification' })

const router = useRouter()
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const arrayList = ref([])

const handleSelect = (row ) => {
  // console.log('删除指标要求', row)
  if(row.selected){
    arrayList.value.push(row.id)
    console.log(arrayList.value,'arrayList.value')
  }else {
    arrayList.value = arrayList.value.filter(item => item !== row.id)
    console.log(arrayList.value,'arrayList.value')
  }
}


const loading = ref(false) // 列表的加载中
const total = ref(100) // 列表的总页数

// Mock data


const list = ref()

let queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  type:2,
  checkState:undefined,
  sampleNum:undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  queryParams.samplingStartDate = queryParams.samplingStartDate1? queryParams.samplingStartDate1[0] :''
  queryParams.samplingEndDate = queryParams.samplingStartDate1? queryParams.samplingStartDate1[1] :''
  queryParams.checkFilterStartTime =  queryParams.checkFilterEndTime1 ?queryParams.checkFilterEndTime1[0] :''
  queryParams.checkFilterEndTime = queryParams.checkFilterEndTime1? queryParams.checkFilterEndTime1[1] :''
  loading.value = true
  try {
    const data = await SampleInformationApi.getSampleInformationPage(queryParams)
    list.value = data.list
    
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams = {
  pageNo: 1,
  pageSize: 10,
  type:2,
  samplingStartDate1:undefined,
  checkFilterEndTime1 : undefined,
  sampleNum : undefined,
  sampleName : undefined,
  checkState:undefined
}
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  getList()

}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrganizationQualificationApi.deleteOrganizationQualification(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {

  try {

    if(arrayList.value.length >0) {
  // 导出的二次确认

  const idsArr = arrayList.value.join(';')
  console.log(idsArr,'idsArr')
  await message.exportConfirm()
    // 发起导出
    loading.value = true
    exportLoading.value = true
    const data = await SampleInformationApi.exportSampleInformation({ids:idsArr,type:2, pageNo:1,pageSize:10})
    download.excel(data, '质检平台-检验信息审批.xls')
    } else{
 
        // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    loading.value = true
    exportLoading.value = true
    const data = await SampleInformationApi.exportSampleInformation(queryParams)
    download.excel(data, '质检平台-检验信息审批.xls')
    }

  
  } catch {
  } finally {
    exportLoading.value = false
    loading.value = false
  }
}


const statusList = ref([])
// 获取检验状态字典
const getDictDataPageApi = async ()=>{
 const res = await  getDictDataPage({
    dictType:'check_state_approval'
  })
  console.log(res,'res')
  statusList.value = res.list.map(v=>{
    console.log(v.value,'v')
      return {
      label:v.label,
      value:Number(v.value)
    }

  })
  console.log(res,'状态字典')
}

/** 初始化 **/
onMounted(() => {
  getList()

  getDictDataPageApi()
})

onActivated(()=>{
  getList()

getDictDataPageApi()
})

// Handle row click to view details
const handleViewDetails = (row) => {
  router.push({
    path: `/inspection-info-management/inspection-detail`,
    query: { id: row.id }
  })
}

onDeactivated(()=>{
  resetQuery()
})
</script>


<style lang="scss" scoped>

.table-btn{
  padding: 10px 50px;
}
</style>